package vek.controllers.SearchAndList;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import vek.GUI.SearchAndListView.KeyboardView;
import vek.ObjectDatabase.POIDatabase;
import vek.ObjectDatabase.Search;

/**
 * Handles the user input and database contact in the panel that displays the on-screen keyboard.
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */
public class KeyboardSubPanelController implements ActionListener{
    
    private KeyboardView keyboardView;
    private NavigationPanelController navigationPanelController;
    private Search.SearchType searchType;
    
    public enum TypeOfSearch { NAME, TYPE, PHONENUMBER, ROAD } // the type of search
    
    /**
     * 
     * @param kv the KeyboardView which this controller handles input of
     * @param npc the NavigationPanelController which has access to the database connector.
     */
    
    public KeyboardSubPanelController(KeyboardView kv, NavigationPanelController npc)
    {
        keyboardView = kv;
        navigationPanelController = npc;
    }
    
    /**
     * This method is run when the user clicks on the buttons of the on-screen keyboard.
     * The text of the button is appened to the text field of the specific search type.
     * @param e an ActionEvent created when the user clicks
     */

    public void actionPerformed(ActionEvent e) {
        
        JButton button = (JButton) e.getSource();
        
        keyboardView.appendToTextField(button.getText());
        
        if(searchType == Search.SearchType.NAMESTARTSWITH)
        {
            Search search = navigationPanelController.getCurrentSearch();
            String[] arg = { keyboardView.getTextFieldText() };
            search.addSearch(Search.SearchType.NAMESTARTSWITH, arg);
            navigationPanelController.showInRightPanelFromSearch( search );
        }
        else if(searchType == Search.SearchType.TYPESTARTSWITH)
        {
            Search search = navigationPanelController.getCurrentSearch();
            String[] arg = { keyboardView.getTextFieldText() };
            search.addSearch(Search.SearchType.TYPESTARTSWITH, arg);
            navigationPanelController.showInRightPanelFromSearch( search );
        }
        else if(searchType == Search.SearchType.PHONENUMBERSTARTSWITH)
        {
            Search search = navigationPanelController.getCurrentSearch();
            String[] arg = { keyboardView.getTextFieldText() };
            search.addSearch(Search.SearchType.PHONENUMBERSTARTSWITH, arg);
            navigationPanelController.showInRightPanelFromSearch( search );
        }
        else if(searchType == Search.SearchType.ROADSTARTSWITH)
        {
            Search search = navigationPanelController.getCurrentSearch();
            String[] arg = { keyboardView.getTextFieldText() };
            search.addSearch(Search.SearchType.ROADSTARTSWITH, arg);
            navigationPanelController.showInRightPanelFromSearch( search );
        }
        
        
    }
    
    /**
     * Changes the type of search, happens every time they on-screen keyboard is shown
     * @param tos the type of search for this keyboard
     */
    
    public void changeType(Search.SearchType tos)
    {
        searchType = tos;
    }
    
    /**
     * Gets the type of search for this keyboard
     * @return the type of search for this keyboard
     */
    
    public Search.SearchType getSearchType()
    {
        return searchType;
    }

}
